﻿using Communication.Model.configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Config
{
    public class ConfigHelper
    {
        Settings settings = new Settings();
        //string filepath = "settings.xml";
        public void struct2xml(string filepath, ACQParametersConfig config)
        {
            settings.Open(filepath);
            //配置振动通道
            for (int i = 0; i < config.ChannelConfig_Data.Length; i++)
            {
                //基本信息
                settings.ChannelConfig.Omaplchannels.Channels[i].ChannelName = config.ChannelConfig_Data[i].ChannelName.ToString();
                settings.ChannelConfig.Omaplchannels.Channels[i].Enable = config.ChannelConfig_Data[i].enable;
                settings.ChannelConfig.Omaplchannels.Channels[i].SpeedNo = config.ChannelConfig_Data[i].SpeedNum;
                //settings.ChannelConfig.Omaplchannels.Channels[i].Alarm
                settings.ChannelConfig.Omaplchannels.Channels[i].FixAngle = config.ChannelConfig_Data[i].InstallationAngle;
                settings.ChannelConfig.Omaplchannels.Channels[i].SensorType = config.ChannelConfig_Data[i].SensorType;
                settings.ChannelConfig.Omaplchannels.Channels[i].Sensitivity = config.ChannelConfig_Data[i].Sensitivity;
                settings.ChannelConfig.Omaplchannels.Channels[i].AdjustFactor = config.ChannelConfig_Data[i].Linearity;
                settings.ChannelConfig.Omaplchannels.Channels[i].ZeroOffset = config.ChannelConfig_Data[i].ZeroOffset;
                settings.ChannelConfig.Omaplchannels.Channels[i].PianYiLiang = config.ChannelConfig_Data[i].OffsetValue;
                //可选频段名称
                settings.ChannelConfig.Omaplchannels.Channels[i].OptionPin1Name = config.ChannelConfig_Data[i].Band1.ToString();
                settings.ChannelConfig.Omaplchannels.Channels[i].OptionPin2Name = config.ChannelConfig_Data[i].Band2.ToString();
                settings.ChannelConfig.Omaplchannels.Channels[i].OptionPin3Name = config.ChannelConfig_Data[i].Band3.ToString();
                //可选频段1
                settings.ChannelConfig.Omaplchannels.Channels[i].OptionPin1Enable = config.ChannelConfig_Data[i].Band1IsUser;
                settings.ChannelConfig.Omaplchannels.Channels[i].OptionPin1UpThreshold = config.ChannelConfig_Data[i].ULimit1;
                settings.ChannelConfig.Omaplchannels.Channels[i].OptionPin1DownThreshold = config.ChannelConfig_Data[i].LLimit1;
                settings.ChannelConfig.Omaplchannels.Channels[i].OptionPin1Warning = config.ChannelConfig_Data[i].Band1YellowValue;
                settings.ChannelConfig.Omaplchannels.Channels[i].OptionPin1Alarm = config.ChannelConfig_Data[i].Band1RedValue;
                //可选频段2
                settings.ChannelConfig.Omaplchannels.Channels[i].OptionPin2Enable = config.ChannelConfig_Data[i].Band2IsUser;
                settings.ChannelConfig.Omaplchannels.Channels[i].OptionPin2UpThreshold = config.ChannelConfig_Data[i].ULimit2;
                settings.ChannelConfig.Omaplchannels.Channels[i].OptionPin2DownThreshold = config.ChannelConfig_Data[i].LLimit2;
                settings.ChannelConfig.Omaplchannels.Channels[i].OptionPin2Warning = config.ChannelConfig_Data[i].Band2YellowValue;
                settings.ChannelConfig.Omaplchannels.Channels[i].OptionPin2Alarm = config.ChannelConfig_Data[i].Band2RedValue;
                //可选频段3
                settings.ChannelConfig.Omaplchannels.Channels[i].OptionPin3Enable = config.ChannelConfig_Data[i].Band3IsUser;
                settings.ChannelConfig.Omaplchannels.Channels[i].OptionPin3UpThreshold = config.ChannelConfig_Data[i].ULimit3;
                settings.ChannelConfig.Omaplchannels.Channels[i].OptionPin3DownThreshold = config.ChannelConfig_Data[i].LLimit3;
                settings.ChannelConfig.Omaplchannels.Channels[i].OptionPin3Warning = config.ChannelConfig_Data[i].Band3YellowValue;
                settings.ChannelConfig.Omaplchannels.Channels[i].OptionPin3Alarm = config.ChannelConfig_Data[i].Band3RedValue;
                //一倍频配置
                settings.ChannelConfig.Omaplchannels.Channels[i].Frequency1Enable = config.ChannelConfig_Data[i].OneAmplitudeIsUser;
                settings.ChannelConfig.Omaplchannels.Channels[i].Frequency1Warning = config.ChannelConfig_Data[i].OneAmplitudeYellowValue;
                settings.ChannelConfig.Omaplchannels.Channels[i].Frequency1Alarm = config.ChannelConfig_Data[i].OneAmplitudeRedValue;
                //二倍频配置
                settings.ChannelConfig.Omaplchannels.Channels[i].Frequency2Enable = config.ChannelConfig_Data[i].TwoAmplitudeIsUser;
                settings.ChannelConfig.Omaplchannels.Channels[i].Frequency2Warning = config.ChannelConfig_Data[i].TwoAmplitudeYellowValue;
                settings.ChannelConfig.Omaplchannels.Channels[i].Frequency2Alarm = config.ChannelConfig_Data[i].TwoAmplitudeRedValue;
                //三倍频配置
                settings.ChannelConfig.Omaplchannels.Channels[i].FrequencyHalfEnable = config.ChannelConfig_Data[i].HalfAmplitudeIsUser;
                settings.ChannelConfig.Omaplchannels.Channels[i].FrequencyHalfWarning = config.ChannelConfig_Data[i].HalfAmplitudeYellowValue;
                settings.ChannelConfig.Omaplchannels.Channels[i].FrequencyHalfAlarm = config.ChannelConfig_Data[i].HalfAmplitudeRedValue;
                //加速度高频段阈值
                settings.ChannelConfig.Omaplchannels.Channels[i].AccHighAmplitudeValueIsUser = config.ChannelConfig_Data[i].AccHighAmplitudeIsUser;
                settings.ChannelConfig.Omaplchannels.Channels[i].AccHighAmplitudeYellowValue = config.ChannelConfig_Data[i].AccHighAmplitudeYellowValue;
                settings.ChannelConfig.Omaplchannels.Channels[i].AccHighAmplitudeRedValue = config.ChannelConfig_Data[i].AccHighAmplitudeRedValue;
                //加速度低频段阈值
                settings.ChannelConfig.Omaplchannels.Channels[i].AccLowAmplitudeValueIsUser = config.ChannelConfig_Data[i].AccLowAmplitudeIsUser;
                settings.ChannelConfig.Omaplchannels.Channels[i].AccLowAmplitudeYellowValue = config.ChannelConfig_Data[i].AccLowAmplitudeYellowValue;
                settings.ChannelConfig.Omaplchannels.Channels[i].AccLowAmplitudeRedValue = config.ChannelConfig_Data[i].AccLowAmplitudeRedValue;
                //加速度全频段阈值
                settings.ChannelConfig.Omaplchannels.Channels[i].AccTotalAmplitudeValueIsUser = config.ChannelConfig_Data[i].AccTotalAmplitudeIsUser;
                settings.ChannelConfig.Omaplchannels.Channels[i].AccTotalAmplitudeYellowValue = config.ChannelConfig_Data[i].AccTotalAmplitudeYellowValue;
                settings.ChannelConfig.Omaplchannels.Channels[i].AccTotalAmplitudeRedValue = config.ChannelConfig_Data[i].AccTotalAmplitudeRedValue;
            }
            //转速通道配置
            for (int i = 0; i < config.SpeedConfigt_Data.Length; i++)
            {
                //基本信息
                settings.ChannelConfig.omplconfig_rotationchannels.Channels[i].ChannelName = config.SpeedConfigt_Data[i].KeyPhaseName.ToString();
                settings.ChannelConfig.omplconfig_rotationchannels.Channels[i].Enable = config.SpeedConfigt_Data[i].enable;
                settings.ChannelConfig.omplconfig_rotationchannels.Channels[i].rationdir = config.SpeedConfigt_Data[i].RotationDirection;
                //键相槽
                //settings.ChannelConfig.omplconfig_rotationchannels.Channels[i].JianXiangCao = config.SpeedConfigt_Data[i].;
                settings.ChannelConfig.omplconfig_rotationchannels.Channels[i].JianXiangCaoAuto = config.SpeedConfigt_Data[i].RunMod;
                settings.ChannelConfig.omplconfig_rotationchannels.Channels[i].JianXiangCaoMax = config.SpeedConfigt_Data[i].KeySlotMax;
                settings.ChannelConfig.omplconfig_rotationchannels.Channels[i].JianXiangCaoMin = config.SpeedConfigt_Data[i].KeySlotMin;
                settings.ChannelConfig.omplconfig_rotationchannels.Channels[i].JianXiangCaoVolCompare = config.SpeedConfigt_Data[i].CompareVol;
                //齿轮盘
                settings.ChannelConfig.omplconfig_rotationchannels.Channels[i].GearMax = config.SpeedConfigt_Data[i].GearKey;
                settings.ChannelConfig.omplconfig_rotationchannels.Channels[i].GearMin = config.SpeedConfigt_Data[i].GearMin;
                settings.ChannelConfig.omplconfig_rotationchannels.Channels[i].ChiLunPanNum = config.SpeedConfigt_Data[i].GearTeethNum;
            }
            //测点配置
            for (int i = 0; i < config.TestPoint_Data.Length; i++)
            {
                settings.ChannelConfig.omplconfig_testpos.TestPoints[i].TestPointName = config.TestPoint_Data[i].TestPointName.ToString();
                settings.ChannelConfig.omplconfig_testpos.TestPoints[i].Enable = config.TestPoint_Data[i].enable;
                settings.ChannelConfig.omplconfig_testpos.TestPoints[i].channelxindex = config.TestPoint_Data[i].TestPointXChannelID;
                settings.ChannelConfig.omplconfig_testpos.TestPoints[i].channelyindex = config.TestPoint_Data[i].TestPointYChannelID;
                settings.ChannelConfig.omplconfig_testpos.TestPoints[i].bearhousx = config.TestPoint_Data[i].bearhousx;
                settings.ChannelConfig.omplconfig_testpos.TestPoints[i].bearhousy = config.TestPoint_Data[i].bearhousy;
                settings.ChannelConfig.omplconfig_testpos.TestPoints[i].iniaxlex = config.TestPoint_Data[i].iniaxlex;
                settings.ChannelConfig.omplconfig_testpos.TestPoints[i].iniaxley = config.TestPoint_Data[i].iniaxley;
                settings.ChannelConfig.omplconfig_testpos.TestPoints[i].bearhori = config.TestPoint_Data[i].bearhori;
                settings.ChannelConfig.omplconfig_testpos.TestPoints[i].bearveri = config.TestPoint_Data[i].bearveri;
            }
            //三向测点配置
            for (int i = 0; i < config.TriAccTestPointConfig_Data.Length; i++)
            {
                ///TODO：问题
                //settings.ChannelConfig.omplconfig_testpos_shock.TestPoint3Ds[i].Name = config.TriAccTestPointConfig_Data[i].TestpointName.ToString();
                settings.ChannelConfig.omplconfig_testpos_shock.TestPoint3Ds[i].Enable = config.TriAccTestPointConfig_Data[i].enable;
                settings.ChannelConfig.omplconfig_testpos_shock.TestPoint3Ds[i].channelid = i + 1;
                settings.ChannelConfig.omplconfig_testpos_shock.TestPoint3Ds[i].channelindexx = config.TriAccTestPointConfig_Data[i].TestPointXChannelID;
                settings.ChannelConfig.omplconfig_testpos_shock.TestPoint3Ds[i].channelindexy = config.TriAccTestPointConfig_Data[i].TestPointYChannelID;
                settings.ChannelConfig.omplconfig_testpos_shock.TestPoint3Ds[i].channelindexz = config.TriAccTestPointConfig_Data[i].TestPointZChannelID;
            }

            #region [系统配置]
            settings.SystemSet.IP1 = int.Parse(config.IPAddress[0].ToString());
            settings.SystemSet.IP2 = int.Parse(config.IPAddress[1].ToString());
            settings.SystemSet.IP3 = int.Parse(config.IPAddress[2].ToString());
            settings.SystemSet.IP4 = int.Parse(config.IPAddress[3].ToString());
            settings.SystemSet.Port = int.Parse(config.ClientPortNum.ToString());
            settings.SystemSet.nRefreshIndex = int.Parse(config.UIRefreshFrequency.ToString());
            settings.SystemSet.nSampleMode = int.Parse(config.AcquisitionType.ToString());
            #endregion

            #region [存储设置]
            settings.StoreSet.HourInterval = int.Parse(config.HourDBInterval.ToString());
            settings.StoreSet.DayInterval = int.Parse(config.DayDBInterval.ToString());
            settings.StoreSet.WeekInterval = int.Parse(config.WeekDBInterval.ToString());
            settings.StoreSet.MonthInterval = int.Parse(config.MonthDBInterval.ToString());
            settings.StoreSet.YearInterval = int.Parse(config.YearDBInterval.ToString());
            settings.StoreSet.Store = int.Parse(config.LocalSave.ToString());
            settings.StoreSet.StartSpeed = int.Parse(config.StartSpeed.ToString());
            settings.StoreSet.StopSpeed = int.Parse(config.StopSpeed.ToString());
            settings.StoreSet.BlackBoxUpLimit = int.Parse(config.BlackBoxSUpLimit.ToString());
            settings.StoreSet.BlackBoxSDownLimit = int.Parse(config.BlackBoxSDownLimit.ToString());
            settings.StoreSet.BlackboxBeforeEvent = int.Parse(config.BlackboxBeforeEvent.ToString());
            settings.StoreSet.BlackboxAfterEvent = int.Parse(config.BlackboxAfterEvent.ToString());
            settings.StoreSet.BlackBoxSavFre = int.Parse(config.BlackBoxSavFre.ToString());
            settings.StoreSet.IndexBiaHe = int.Parse(config.VariableLoadDB.ToString());
            #endregion

            settings.saveXml(filepath);
        }

    }
}
